package com.goodmr.passport.webapp.controller;

import com.goodmr.passport.webapp.Serivce.LoginService;
import com.goodmr.passport.webapp.model.request.SigninCodeReq;
import com.goodmr.passport.webapp.model.request.SigninPwdReq;
import com.goodmr.passport.webapp.model.response.SigninResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 帐号登录
 *
 * @author <a href="mailto:bingdong13@msn.com">bigo</a>
 */
@RestController
@RequestMapping(path = "/passport/signin")
public class SigninController {

    @Autowired
    @Qualifier("loginPwdService")
    private LoginService loginPwdService;

    @Autowired
    @Qualifier("loginCodeService")
    private LoginService loginCodeService;

    /**
     * 通过帐号密码登录
     *
     * @param body 请求数据包
     * @return 登录成功返回 token，否则返回异常
     */
    @PatchMapping("")
    public ResponseEntity<SigninResp> loginByPassword(@Valid @RequestBody SigninPwdReq body) {

        SigninResp response = loginPwdService.login(body);

        return ResponseEntity.ok(response);
    }

    /**
     * 通过帐号验证码登录
     *
     * @param body 请求数据包
     * @return 登录成功返回 token，否则返回异常
     */
    @PostMapping("/code")
    public ResponseEntity<SigninResp> loginByCode(@Valid @RequestBody SigninCodeReq body) {

        SigninResp response = loginCodeService.login(body);

        return ResponseEntity.ok(response);
    }
}
